package fan.com.httpframe.http.requst;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by fanliang on 18/5/27.
 */

public class RequestBodyUtils {

    public static final String LOGIN = "login";
    public static final String GET_HOME_NEWS_IMG = "getHomeNewsImg";
    public static final String normalStr = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xzs=\"http://xzspos.webservices.platform.powerrich.com\">\n" +
            "     <soapenv:Header/>\n" +
            "     <soapenv:Body>\n" +
            "     </soapenv:Body>\n" +
            "     </soapenv:Envelope>";

    public static StringBuilder getNewUrlSb(String action,List<String> list){
        int index = normalStr.indexOf("<soapenv:Body>\n")+"<soapenv:Body>\n".length();

        StringBuilder normalSb = new StringBuilder(normalStr);

        StringBuilder actionSb = new StringBuilder("<xzs:"+action+">\n</xzs:"+action+">\n");
        StringBuilder paramSb = new StringBuilder();
        if(list != null) {
            for (int i = 0; i < list.size(); i++) {
                paramSb.append("<xzs:in" + i + ">" + list.get(i) + "</xzs:in" + i + ">\n");
            }
            actionSb.insert(("<xzs:"+action+">\n").length(),paramSb.toString());
        }
        StringBuilder newSb = normalSb.insert(index, actionSb.toString());
        return newSb;
    }
    public static MediaType parse(){
        return MediaType.parse("text/xml;charset=UTF-8");
    }


    public static RequestBody login(List<String> list){
        return RequestBody.create(parse(),getNewUrlSb(LOGIN,list).toString());
    };

    public static RequestBody getHomeNewsImg(){
        return RequestBody.create(parse(),getNewUrlSb(GET_HOME_NEWS_IMG,null).toString());
    }



}
